package DOM4J;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;

public class Demo1 {

	public void read() throws DocumentException {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book1.xml"));
		Element root = document.getRootElement();
		Element bookname = root.element("书").element("书名");
		System.out.println(bookname.getText());
	}

	public void readAttribute() throws DocumentException {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book1.xml"));
		Element root = document.getRootElement();
		String value = root.element("书").attributeValue("name");
		System.out.println(value);
	}

	public void add() throws DocumentException, IOException {// 添加
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book1.xml"));
		Element price = DocumentHelper.createElement("售价");
		price.setText("12元");
		document.getRootElement().element("书").add(price);
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		// XMLWriter writer = new XMLWriter(new FileWriter("src/book1.xml"));
		XMLWriter writer = new XMLWriter(new FileOutputStream(new File(
				"src/book1.xml")), format);
		writer.write(document);
		writer.close();
	}

	public void add2() throws Exception {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book1.xml"));
		Element price = DocumentHelper.createElement("售价");
		price.setText("399元");
		List list = document.getRootElement().element("书").elements();
		list.add(1, price);
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		XMLWriter writer = new XMLWriter(new FileOutputStream("src/book1.xml"),
				format);
		writer.write(document); // utf-8
		writer.close();
	}

	public void update() throws DocumentException, IOException {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book1.xml"));
		Element root = document.getRootElement();
		Element price = (Element) root.element("书").elements("售价").get(2);
		price.setText("209元");
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		// XMLWriter writer = new XMLWriter(new FileWriter("src/book1.xml"));
		XMLWriter writer = new XMLWriter(new FileOutputStream(new File(
				"src/book1.xml")), format);
		writer.write(document);
		writer.close();
	}

	public void delete() throws DocumentException, IOException {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/book1.xml"));
		Element root = document.getRootElement();
		Element price = (Element) root.element("书").elements("售价").get(1);
		price.getParent().remove(price);
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		// XMLWriter writer = new XMLWriter(new FileWriter("src/book1.xml"));
		XMLWriter writer = new XMLWriter(new FileOutputStream(new File(
				"src/book1.xml")), format);
		writer.write(document);
		writer.close();
	}

}
